package com.chen.service.Impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.common.Result;
import com.chen.mapper.UserMapper;
import com.chen.pojo.User;
import com.chen.pojo.bean.UserInfo;
import com.chen.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import java.util.Date;
import java.util.List;


@Service
@SuppressWarnings("all")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 查询所有用户
     *
     * @param
     * @return
     */
    public Result<?> userAll() {
        List<User> userList = userMapper.selectList(null);
        return Result.success(userList);
    }

    /**
     * 分页
     *
     * @param pageNum  当前页
     * @param pageSize 每页的个数
     * @param status   查询的状态
     * @param search   根据什么搜索
     * @return
     */
    public Result<?> findPage(Integer pageNum, Integer pageSize, Integer status, String search) {
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        wrapper.eq(User::getStatus, status);
        //根据注册的用户进行查询  不为空
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(User::getUsername, search);
        }
        return Result.success(userMapper.selectPage(new Page<>(pageNum, pageSize, status), wrapper));
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    public Result<?> addUser(User user) {
        //默认给一个密码123456
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        user.setStatus(1); //默认审核通过
        user.setRegtime(new Date());
        return Result.success(userMapper.insert(user));
    }

    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    public Result<?> updateUser(UserInfo user) {
        User updateUser = new User(user);
        userMapper.updateById(updateUser);
        return Result.success();
    }

    /**
     * 删除用户根据id
     *
     * @param id
     * @return
     */
    public Result deleteUser(Long id) {

        return Result.success(userMapper.deleteById(id));
    }

    /**
     * 管理员审核
     * @param user
     * @return
     */
    public Result<?> agree(User user){
        user.setStatus(1);
        userMapper.updateById(user);
        return Result.success(user);
    }

    @Override
    public List<User> findMember() {
        return userMapper.findMember();
    }

    @Override
    public List<User> findNumbers() {
        return userMapper.findNumbers();
    }
}
